﻿$(function () {
    $("#department-grid").treegrid({
        url: $.Api("Sys/Department/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        idField: "ID", toolbar: "#department-tools",
        treeField: "Name", parentField: "Parent",
        columns:
        [[
            { field: "Name", title: "名称", width: 200, align: "left" },
            { field: "Remark", title: "描述", width: 280, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" }
        ]]
    });
});

var Department = {
    //添加
    Insert: function () {
        var row = $("#department-grid").treegrid("getSelected");
        if (row) {
            Department.Mutual("添加", "sys/department/Edit.aspx?pid=" + row.ID);
        }
        else { Department.Mutual("添加", "sys/department/Edit.aspx"); }
    },
    //修改
    Update: function () {
        var row = $("#department-grid").treegrid("getSelected");
        if (row) {
            Department.Mutual("修改", "sys/department/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#department-grid").treegrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/Department/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Department.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#department-model",
                    url: $.Api("Sys/Department/Save"),
                    success: function (result) { Department.Reload(); }
                });
            },
            onLoad: function () { Department.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#department-parent").combotree({
            width: 150, url: $.Api("Sys/Department/ComboTree"),
            onLoadSuccess: function (node, data) {
                var nodeid = $("#department-id").val();
                if (nodeid) {
                    var etree = $("#department-parent").combotree("tree");
                    etree.tree("remove", etree.tree('find', nodeid).target);
                }
            }
        });
    },

    //排序向上
    Up: function () {
        Department.Order("up");
    },
    //排序向下
    Down: function () {
        Department.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#department-grid").treegrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Department/SetOrder"), type: "post",
                data: { id: row.ID, parent: row.Parent, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Department.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.TreeGridReload("#department-grid");
    }
};